package com.btpj.module_search.ui

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.btpj.jetpack.base.BaseViewModel
import com.btpj.jetpack.utils.ToastUtil
import com.btpj.jetpack.entity.ApiResult
import com.btpj.module_search.data.entity.HotKeyModel
import com.btpj.module_search.data.remote.SearchRepository
import kotlinx.coroutines.launch

class HotKeyViewModel(private val repository: SearchRepository) : BaseViewModel() {

    val hotKeyLiveData = MutableLiveData<MutableList<HotKeyModel>>()

    override fun start() {
        getHotKey()
    }

    private fun getHotKey() {
        viewModelScope.launch {
            when (val result = repository.getHotKey()) {
                is ApiResult.Success -> hotKeyLiveData.postValue(result.data)
                is ApiResult.Error -> ToastUtil.showShort(result.exception.errorMsg ?: "请求失败")
            }
        }
    }
}